package com.example.video_player

import androidx.annotation.NonNull

import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result

/** VideoPlayerPlugin */
class VideoPlayerPlugin : FlutterPlugin, MethodCallHandler, PigeonDemoMessage.Api {
    private lateinit var channel: MethodChannel

    override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
        channel = MethodChannel(flutterPluginBinding.binaryMessenger, "video_player")
        channel.setMethodCallHandler(this)

        /***
         * when error,use this method:
         *   https://www.jianshu.com/p/1f3ee565f4e7
         */
        // register
        PigeonDemoMessage.Api.setup(flutterPluginBinding.binaryMessenger, this)
        LiviBridgeService.LiviBridgeServiceApi.setup(
            flutterPluginBinding.binaryMessenger,
            LiviBridgeServiceImpl()
        )
    }

    override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
        if (call.method == "getPlatformVersion") {
            result.success("Android ${android.os.Build.VERSION.RELEASE}")
        } else {
            result.notImplemented()
        }
    }

    override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
        channel.setMethodCallHandler(null)
    }

    /**
     * implement method
     */
    override fun search(request: PigeonDemoMessage.SearchRequest?): PigeonDemoMessage.SearchReply {
        var result = PigeonDemoMessage.SearchReply()
        result.result = "error msg";
        if (request != null) {
            var query = request.query;
            if ("requestMessage" == query) {
                result.result = "search has been sucess";
            }
        }
        return result;
    }
}
